package com.dld.android.util;

import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import android.util.Log;

import com.dld.coupon.util.XmlUtil;
import com.dld.protocol.json.BankCouponDetail;
import com.dld.protocol.json.GroupDetail;
import com.dld.protocol.json.Shop;
import com.dld.protocol.json.Ticket;

public class ReflectionFactory {
	public static <T> void attach(T paramT, Node paramNode, Class<?> paramClass)
			throws Exception {
		Element localElement = (Element) paramNode;
		Field[] arrayOfField = paramClass.getDeclaredFields();

		for (int i = 0; i < arrayOfField.length; i++) {

			Field localField = arrayOfField[i];
			localField.setAccessible(true);
			try {
				String str2 = localField.getType().getName();
				String str1 = XmlUtil.getString(localElement, localField
						.getName());
				if (!str1.equals(""))
					if (str2.equals(String.class.getName()))
						localField.set(paramT, str1);
					else if (str2.equals("int"))
						localField.set(paramT, Integer.valueOf(Integer
								.parseInt(str1)));
					else if (str2.equals("float"))
						localField.set(paramT, Float.valueOf(Float
								.parseFloat(str1)));
					else if (str2.equals("double"))
						localField.set(paramT, Double.valueOf(Double
								.parseDouble(str1)));
					else if (str2.equals("boolean"))
						localField.set(paramT, Boolean.valueOf(Boolean
								.parseBoolean(str1)));
					else if (str2.equals("long"))
						localField.set(paramT, Long.valueOf(Long
								.parseLong(str1)));
			} catch (Exception e) {
				//e.printStackTrace();
			}
		}
	}

	public static <T> T create(JSONObject paramJSONObject, Class<?> paramClass) {
		return null;
	}

	public static <T> T create(Node paramNode, Class<?> paramClass)
			throws Exception {
		Object localObject = paramClass.newInstance();
		attach(localObject, paramNode, paramClass);
		return (T) localObject;
	}

	public static JSONObject toJSON(Object paramObject) {
		JSONObject localJSONObject = new JSONObject();
		Field[] arrayOfField = paramObject.getClass().getFields();
		int j = arrayOfField.length;
		int i = 0;
		while (true) {
			if (i >= j)
				return localJSONObject;
			Object localObject = arrayOfField[i];
			try {
				String str = ((Field) localObject).getName();
				JSONArray localJSONArray;
				if (str.equals("shops")) {
					localJSONArray = new JSONArray();
					localObject = ((List) ((Field) localObject)
							.get(paramObject)).iterator();
					while (true) {
						if (!((Iterator) localObject).hasNext()) {
							localJSONObject.put(str, localJSONArray);
							break;
						}
						localJSONArray
								.put(toJSON((Shop) ((Iterator) localObject)
										.next()));
					}
				}
				if (str.equals("tickets")) {
					localJSONArray = new JSONArray();
					localObject = ((List) ((Field) localObject)
							.get(paramObject)).iterator();
					while (true) {
						if (!((Iterator) localObject).hasNext()) {
							localJSONObject.put(str, localJSONArray);
							break;
						}
						localJSONArray
								.put(toJSON((Ticket) ((Iterator) localObject)
										.next()));
					}
				}
				if (str.equals("groups")) {
					localJSONArray = new JSONArray();
					localObject = ((List) ((Field) localObject)
							.get(paramObject)).iterator();
					while (true) {
						if (!((Iterator) localObject).hasNext()) {
							localJSONObject.put(str, localJSONArray);
							break;
						}
						localJSONArray
								.put(toJSON((GroupDetail) ((Iterator) localObject)
										.next()));
					}
				}
				if (str.equals("banks")) {
					localJSONArray = new JSONArray();
					localObject = ((List) ((Field) localObject)
							.get(paramObject)).iterator();
					while (true) {
						if (!((Iterator) localObject).hasNext()) {
							localJSONObject.put(str, localJSONArray);
							break;
						}
						localJSONArray
								.put(toJSON((BankCouponDetail) ((Iterator) localObject)
										.next()));
					}
				}
				localJSONObject
						.put(str, ((Field) localObject).get(paramObject));
			} catch (Exception localException) {
				i++;
			}
		}
	}
}
